package com.sszh.base.service.impl;

import com.google.gson.Gson;
import com.sszh.base.configurer.token.UserTokenManager;
import com.sszh.base.controller.model.LoginRequestDTO;
import com.sszh.base.controller.model.LoginResponseDTO;
import com.sszh.base.controller.model.RequestDTO;
import com.sszh.base.controller.model.ResponseDTO;
import com.sszh.base.mapper.UserMapper;
import com.sszh.base.mapper.domain.UserDO;
import com.sszh.base.service.UserService;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author WangJianGuo
 * @Title: UserServiceImpl
 * @Package com.sszh.base.service.impl
 * @Description: 用户服务实现类
 * @date 2018/3/30/030 10:47
 */
@Log4j
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    UserMapper userMapper;

    @Override
    public UserDO findById(int id) {
        log.info("findById服务层开启工作");
        UserDO user;
        user = userMapper.getUserById(id);
        log.info(userMapper.getUserById(id).toString());
        return user;
    }

    @Override
    public ResponseDTO<LoginResponseDTO> login(RequestDTO<LoginRequestDTO> requestModel){
        log.info("login服务层开启工作");
        UserDO user;
        String account = requestModel.getPosts().getAccount();
        String password = requestModel.getPosts().getPassword();

        //数据库查询
        user = userMapper.getUserByAccountPass(account, password);
        if(user == null){
            return null;
        }else {
            log.info(user);
            LoginResponseDTO loginResponseModel = new LoginResponseDTO();
            loginResponseModel.setPermission(user.getPermission());
            String token = UserTokenManager.getTokenToUser(user);
            loginResponseModel.setToken(token);
            ResponseDTO<LoginResponseDTO> responseModelRequestModel = new ResponseDTO<>(loginResponseModel);
            log.info("返回gson" + new Gson().toJson(responseModelRequestModel));
            return responseModelRequestModel;
        }


    }
}